// DialogInteractionsMatrix.cpp : implementation file
//

#include "stdafx.h"
#include "testSVG.h"
#include "DialogInteractionsMatrix.h"
#include "testSVGDlg.h"

extern MOLECULE* GetNodeBasedOnNumber(int i);
extern void SortMoleculeListBasedOnConnectivity(void);
extern void SortMoleculeListBasedOnMachines (void);
extern MOLECULE* MoleculesList;
extern int NumberOfMolecules;
extern INTERACTION* InteractionsList;
extern bool dispMatrix[1000][1000];
const int WINDOWSIZE=510;

CDC* DialogDC;

// CDialogInteractionsMatrix dialog

IMPLEMENT_DYNAMIC(CDialogInteractionsMatrix, CDialog)
CDialogInteractionsMatrix::CDialogInteractionsMatrix(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogInteractionsMatrix::IDD, pParent)
{
}

CDialogInteractionsMatrix::~CDialogInteractionsMatrix()
{
}

void CDialogInteractionsMatrix::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CDialogInteractionsMatrix, CDialog)
	ON_WM_PAINT()
END_MESSAGE_MAP()


// CDialogInteractionsMatrix message handlers

void CDialogInteractionsMatrix::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	
	MOLECULE* tempNode1;
	int j;
	RECT rect;
    CDC dcMemory;
	CBitmap bitmap;
	CBrush myBrush;
	CPen myPen;

	SortMoleculeListBasedOnMachines();



	dcMemory.CreateCompatibleDC(DialogDC);
	bitmap.CreateCompatibleBitmap(DialogDC, WINDOWSIZE, WINDOWSIZE);	


	rect.left = 0;
	rect.top = 0;
	rect.right =  WINDOWSIZE;//NumberOfMolecules*2;
	rect.bottom = WINDOWSIZE;//NumberOfMolecules*2;

	dcMemory.SelectObject(&bitmap);

	// clear the previous picture
	FillRect(DialogDC->m_hDC, &rect,(HBRUSH) GetStockObject(WHITE_BRUSH));


	tempNode1 = MoleculesList;

	j = 0;


	double m=0.0,n=0.0;
	double nextstep=double(NumberOfMolecules/100);
	bool draw;
	double tempw=WINDOWSIZE;
	double step=double(tempw/NumberOfMolecules);
		for (int i=0;i<NumberOfMolecules-1;i++)
		{
			for (int j=0;j<NumberOfMolecules-1;j++)
			{
				draw=true;
				
				if (i==j)
				{
					myBrush.CreateSolidBrush(RGB(200, 0, 0));
					myPen.CreatePen(PS_NULL, 0, RGB(0, 100, 0));
				}
				else if (dispMatrix[i][j])
				{
					myBrush.CreateSolidBrush(RGB(0, 0, 0));
					myPen.CreatePen(PS_NULL, 0, RGB(0, 100, 0));
				}
				else if (!dispMatrix[i][j])
				{
					myBrush.CreateSolidBrush(RGB(216, 216, 216));
					myPen.CreatePen(PS_NULL, 0, RGB(0, 100, 0));
					draw=false;
				}

				DialogDC->SelectObject( &myBrush );
				DialogDC->SelectObject( &myPen );

				rect.left = (LONG) m; 
				rect.top = (LONG) n;
				if (NumberOfMolecules>200)
				{
					rect.right = (LONG) (m + double (NumberOfMolecules/100)*step);
					rect.bottom = (LONG) (n + double (NumberOfMolecules/100)*step);
				}
				else
				{
					rect.right = (LONG) (m + step);//double(NumberOfMolecules/100)*step;
					rect.bottom = (LONG) (n + step);//double(NumberOfMolecules/100)*step;
				}
			if (draw)
				Rectangle(DialogDC->m_hDC, rect.left, rect.top, rect.right, rect.bottom);
			myBrush.DeleteObject();
			myPen.DeleteObject();
			m+=step;//double(NumberOfMolecules/500)*step;
			}
			m=0;
			n+=step;//double(NumberOfMolecules/500)*step;
		}
	/*CPaintDC dc(this); // device context for painting
	
	MOLECULE* tempNode1;
	MOLECULE* tempNode2;
	INTERACTION* tempInt;

	int i, j, k;
	int flag;
	int flag1;
	int string[1000];
	RECT rect;
    CDC dcMemory;
	CBitmap bitmap;
	CBrush myBrush;
	CPen myPen;

	SortMoleculeListBasedOnMachines();

	//SortMoleculeListBasedOnConnectivity();

	dcMemory.CreateCompatibleDC(DialogDC);
	bitmap.CreateCompatibleBitmap(DialogDC, 1000, 500);	


	rect.left = 0;
	rect.top = 0;
	rect.right =  1000;
	rect.bottom = 500;

	dcMemory.SelectObject(&bitmap);

	// clear the previous picture
	FillRect(DialogDC->m_hDC, &rect,(HBRUSH) GetStockObject(WHITE_BRUSH));


	tempNode1 = MoleculesList;

	j = 0;

	while (tempNode1)
	{
		switch (tempNode1->machine)
		{
			case CENTRAL_SIGNALING:
				myBrush.CreateSolidBrush(RGB(200, 200, 250));
				myPen.CreatePen(PS_SOLID, 2, RGB(200, 200, 250));
				break;
			case TRANSMEMBRANE:
				myBrush.CreateSolidBrush(RGB(200, 250, 200));
				myPen.CreatePen(PS_SOLID, 2, RGB(200, 250, 200));
				break;
			case EXTRACELLULAR_LIGANDS:
				myBrush.CreateSolidBrush(RGB(60, 60, 60));
				myPen.CreatePen(PS_SOLID, 2, RGB(60, 60, 60));
				break;
			case APOPTOSIS:
				myBrush.CreateSolidBrush(RGB(250, 240, 230));
				myPen.CreatePen(PS_SOLID, 2, RGB(250, 240, 230));
				break;
			case TRANSMISSION:
				myBrush.CreateSolidBrush(RGB(0, 100, 0));
				myPen.CreatePen(PS_SOLID, 2, RGB(0, 100, 0));
				break;
			case TRANSLATION:
				myBrush.CreateSolidBrush(RGB(0, 120, 0));
				myPen.CreatePen(PS_SOLID, 2, RGB(0, 120, 0));
				break;
			case TRANSCRIPTION:
				myBrush.CreateSolidBrush(RGB(0, 140, 0));
				myPen.CreatePen(PS_SOLID, 2, RGB(0, 140, 0));
				break;
			case CYTOSKELETON:
				myBrush.CreateSolidBrush(RGB(0, 160, 0));
				myPen.CreatePen(PS_SOLID, 2, RGB(0, 160, 0));
				break;
			case SECRETION:
				myBrush.CreateSolidBrush(RGB(0, 180, 0));
				myPen.CreatePen(PS_SOLID, 2, RGB(0, 180, 0));
				break;
			default:
				myBrush.CreateSolidBrush(RGB(0, 200, 0));
				myPen.CreatePen(PS_SOLID, 2, RGB(0, 200, 0));
				break;
		}

		// apply the modified color to the objects
		DialogDC->SelectObject( &myBrush );
		DialogDC->SelectObject( &myPen );
		//dcMemory.SelectObject( &myBrush );
		//dcMemory.SelectObject( &myPen );	
			
		rect.left = j; 
		rect.top = 0;
		rect.right = j + 2;
		rect.bottom = NumberOfMolecules;
			
		Rectangle(DialogDC->m_hDC, rect.left, rect.top, rect.right, rect.bottom);
		//Ellipse(dcMemory.m_hDC, rect.left, rect.top, rect.right, rect.bottom);
		//Ellipse(DialogDC->m_hDC, rect.left, rect.top, rect.right, rect.bottom);
		// reset the brush and pen for the next circle
		myBrush.DeleteObject();
		myPen.DeleteObject();

		j++;
		tempNode1 = tempNode1->next;
	}

	tempNode1 = MoleculesList;

	k = 0;
	while (tempNode1)
	{
		k++;
		tempNode2 = MoleculesList;
		j = 0;	
		while (tempNode2)
		{
			flag = 0;
			j++;
			for (i = 0; i < tempNode1->linksCount; i++)
			{
				if (tempNode2 == GetNodeBasedOnNumber(tempNode1->linksTo[i]))
				{
					flag = 1;

					tempInt = InteractionsList;

					while (tempInt)
					{
						if (((!strcmp(tempInt->source, tempNode1->name))
							&& 
							(!strcmp(tempInt->target, tempNode2->name))) 
							||
							((!strcmp(tempInt->source, tempNode2->name))
							&& 
							(!strcmp(tempInt->target, tempNode1->name))))
						{
							if (!strcmp(tempInt->effect, "+"))
							{
								flag = 1;
							}
							else if (!strcmp(tempInt->effect, "0"))
							{
								flag = 3;
							}
							else
							{
								flag = 2;
							}
							
							if ((!strcmp(tempInt->typeOfInteraction, "Phosphorylation"))
								||
								(!strcmp(tempInt->typeOfInteraction, "Dephosphorylation")))
							{
								flag1 = 1;
							}
							else if (!strcmp(tempInt->typeOfInteraction, "Binding"))
							{
								flag1 = 2;
							}
							else
							{
								flag1 = 3;
							}

							break;
						}
						tempInt = tempInt->next;
					}
					break;
				}
			}
			
			
			if (flag)
			{
				if (j > k)
				{
					if (flag == 1)
					{
						myBrush.CreateSolidBrush(RGB(0, 0xFF, 0));
						myPen.CreatePen(PS_SOLID, 2, RGB(0, 0xFF, 0));
					}
					else if (flag == 2)
					{
						myBrush.CreateSolidBrush(RGB(0xFF, 0, 0));
						myPen.CreatePen(PS_SOLID, 2, RGB(0xFF, 0, 0));
					}
					else
					{
						myBrush.CreateSolidBrush(RGB(0, 0, 0xFF));
						myPen.CreatePen(PS_SOLID, 2, RGB(0, 0, 0xFF));
					}
				}
				else
				{
					if (flag1 == 1)
					{
						myBrush.CreateSolidBrush(RGB(0, 0xFF, 0xFF));
						myPen.CreatePen(PS_SOLID, 2, RGB(0, 0xFF, 0xFF));
					}
					else if (flag1 == 2)
					{
						myBrush.CreateSolidBrush(RGB(0xFF, 0, 0xFF));
						myPen.CreatePen(PS_SOLID, 2, RGB(0xFF, 0, 0xFF));
					}
					else
					{
						myBrush.CreateSolidBrush(RGB(0, 0, 0));
						myPen.CreatePen(PS_SOLID, 2, RGB(0, 0, 0));
					}
				}

				// apply the modified color to the objects
				DialogDC->SelectObject( &myBrush );
				DialogDC->SelectObject( &myPen );
				//dcMemory.SelectObject( &myBrush );
				//dcMemory.SelectObject( &myPen );	
			
				rect.left = j; 
				rect.top = k;
				rect.right = j + 6;
				rect.bottom = k + 6;
			
				Rectangle(DialogDC->m_hDC, rect.left, rect.top, rect.right, rect.bottom);
				//Ellipse(dcMemory.m_hDC, rect.left, rect.top, rect.right, rect.bottom);
				//Ellipse(DialogDC->m_hDC, rect.left, rect.top, rect.right, rect.bottom);
				// reset the brush and pen for the next circle
				myBrush.DeleteObject();
				myPen.DeleteObject();
			}
			tempNode2 = tempNode2->next;
		}

		tempNode1 = tempNode1->next;	
	}

	//dcMemory.BitBlt(0, 0, 500, 500, DialogDC, 0, 0, BLACKNESS);
	//WriteWindowToDIB("test.bmp", &dcMemory);
	AfxMessageBox("Done!");
	*/
}

BOOL CDialogInteractionsMatrix::OnInitDialog()
{
	CDialog::OnInitDialog();

	DialogDC = GetDC();

	return TRUE;  // return TRUE unless you set the focus to a control
}
